package com.zhijiang.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhijiang.hospital.entity.ComDepartment;
import com.zhijiang.hospital.entity.ComEmployee;
import com.zhijiang.hospital.mapper.ComDepartmentMapper;
import com.zhijiang.hospital.mapper.ComEmployeeMapper;
import com.zhijiang.hospital.service.InformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class InformationServiceImpl implements InformationService {


    @Autowired
    ComDepartmentMapper comDepartmentMapper;
    @Autowired
    ComEmployeeMapper comEmployeeMapper;

    @Override
    public List<ComDepartment> getDepartmentAll() {
        List<ComDepartment> comDepartments = comDepartmentMapper.selectList(null);
        comDepartments.stream().forEach(s -> {
            if (s.getDeptType().equals("C")) {
                s.setDeptType("门诊");
            } else if (s.getDeptType().equals("F")) {
                s.setDeptType("财务");
            } else if (s.getDeptType().equals("L")) {
                s.setDeptType("后勤");
            } else if (s.getDeptType().equals("PI")) {
                s.setDeptType("药库");
            } else if (s.getDeptType().equals("T")) {
                s.setDeptType(" 医技(terminal)");
            } else if (s.getDeptType().equals("O")) {
                s.setDeptType("其它");
            } else if (s.getDeptType().equals("D")) {
                s.setDeptType("机关(department)");
            } else if (s.getDeptType().equals("P")) {
                s.setDeptType("药房");
            } else if (s.getDeptType().equals("N")) {
                s.setDeptType("护士站");
            }

        });
        return comDepartments;
    }

    @Override
    public List<ComEmployee> getEmployee(String deptCode) {
        List<ComEmployee> employees = comEmployeeMapper.selectList(new LambdaQueryWrapper<ComEmployee>().eq(ComEmployee::getDeptCode, deptCode));

        return employees;
    }

    //C 门诊  I  住院  F  财务 L  后勤(logistics)  PI  药库 T 医技(terminal)  O  其它 D  机关(department)P  药房   N 护士站
    @Override
    public List<ComDepartment> getDepartment() {
        //ComEmployee comEmployee = UserHolder.localUser().getComEmployee();
        // List<ComDepartment> comDepartments = comDepartmentMapper.selectList(new LambdaQueryWrapper<ComDepartment>().eq(ComDepartment::getDeptCode, comEmployee.getDeptCode()));
        //comDepartments.stream().forEach(s->{
        //    if (s.getDeptType().equals("C")){
        //        s.setDeptType("门诊");
        //    }else if (s.getDeptType().equals("F")){
        //        s.setDeptType("财务");
        //    }else if (s.getDeptType().equals("L")){
        //        s.setDeptType("后勤");
        //    }else if (s.getDeptType().equals("PI")){
        //        s.setDeptType("药库");
        //    }else if (s.getDeptType().equals("T")){
        //        s.setDeptType(" 医技(terminal)");
        //    }else if (s.getDeptType().equals("O")){
        //        s.setDeptType("其它");
        //    }else if (s.getDeptType().equals("D")){
        //        s.setDeptType("机关(department)");
        //    }else if (s.getDeptType().equals("P")){
        //        s.setDeptType("药房");
        //    }else if (s.getDeptType().equals("N")){
        //        s.setDeptType("护士站");
        //    }
        //
        //});
        // return comDepartments;
        return null;
    }

    @Override
    public List<ComEmployee> getAllEmployee() {
        List<ComEmployee> employees = comEmployeeMapper.selectList(null);
        employees.stream().forEach(s -> {
            if (s.getSexCode().equals("M")) {
                s.setSexCode("男");
            } else if (s.getSexCode().equals("F")) {
                s.setSexCode("女");
            }
            if (s.getValidState().equals("1")) {
                s.setValidState("在用");
            } else if (s.getValidState().equals("2")) {
                s.setValidState("废用");
            } else if (s.getValidState().equals("0")) {
                s.setValidState("停用");
            }
        });
        return employees;
    }

    @Override
    public List<ComEmployee> getDepartmentDown(List<ComDepartment> comDepartments) {
        ArrayList<ComEmployee> comEmployees = new ArrayList<>();
        for (ComDepartment comDepartment : comDepartments) {

            List<ComEmployee> list = comEmployeeMapper.selectList(new LambdaQueryWrapper<ComEmployee>()
                    .eq(ComEmployee::getDeptCode, comDepartment.getDeptCode()));
            comEmployees.addAll(list);

        }

        return comEmployees;
    }

    @Override
    public List<ComDepartment> getDepartmentToEmployees(List<ComEmployee> comEmployees) {
        //ArrayList<ComDepartment> list = new ArrayList<>();
        Set<ComDepartment> set = new HashSet<ComDepartment>();
        for (ComEmployee comEmployee : comEmployees) {
            List<ComDepartment> comDepartments = comDepartmentMapper.selectList(new LambdaQueryWrapper<ComDepartment>()
                    .eq(ComDepartment::getDeptCode, comEmployee.getDeptCode()));
            set.addAll(comDepartments);
        }
        List<ComDepartment> list = set.stream().collect(Collectors.toList());
        return list;
    }


}
